package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author luojia
 * @create 2022-01-01 23:17
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 客户列表分页（名称模糊查询）
     *
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("list")
    public String queryCustomer(@RequestParam("page") Integer page,
                                @RequestParam("rows") Integer rows,
                                @RequestParam(value = "customerName", required = false) String customerName) {
        String customerList = customerService.queryCustomer(page, rows, customerName);
        return customerList;
    }

    /**
     * 客户添加或修改
     *
     * @param customer
     * @param customerId
     * @return
     */
    @PostMapping("save")
    public String supplierAddOrUpdate(Customer customer, @RequestParam(value = "customerId", required = false) Integer customerId) {
        customerService.customerAddOrUpdate(customer, customerId);
        Gson gson = new Gson();
        return gson.toJson(new ServiceVO(100, "请求成功", null));
    }

    /**
     * 客户删除（支持批量删除）
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public String supplierDelete(String ids) {
        String[] split = ids.split(",");
        customerService.customerDelete(split);

        Gson gson = new Gson();
        return gson.toJson(new ServiceVO(100, "请求成功", null));
    }

    /**
     * 客户下拉列表
     * @param name
     * @return
     */
    @PostMapping("/getComboboxList")
    @ResponseBody
    public String getComboboxList(String name){
        List<Customer> customerList = customerService.getComboboxList(name);
        Gson gson = new Gson();
        return gson.toJson(customerList);
    }

}
